aDataSet = [];
deleteImg = '<img id="delete" src="../images/icons/cross.png" />';
updateImg = '<img id="update" src="../images/icons/edit.png" />';
action = "";

window.onload = function() {
	
	var txtLibelleCategorie = $("#txtLibelleCategorie");
	var txtIdCategorie = $("#txtIdCategorie");
	var allFields= $([]).add(txtLibelleCategorie).add(txtIdCategorie);
	
	var aCreateCategorie = $("#aCreateCategorie");
	aCreateCategorie.click(
		function(){
			openDialog("","","create");
				
	});

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}
	
	function openDialog(idCat, libCat, act){
		action = act;
		txtLibelleCategorie.val( txtLibelleCategorie.val() + libCat );
		txtIdCategorie.val( txtIdCategorie.val() + idCat );
		$('#dialog').dialog('open');
	}
	
	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 300,
		modal: true,
		buttons: {
			'Sauvegarder': function() {
				var categorie = {
			            idCategorie: txtIdCategorie.val(),
			            libelleCategorie:txtLibelleCategorie.val()
			        };
				$.ajax({
					type : 'GET',
					url : '/qRH-WS/services/categorie/'+action,
					data : categorie,
					dataType : 'json',
					success : function(result, textStatus, jqXHR) {
						console.log('Success');
						location.reload();
					}
				});
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	

	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}
	}
	
	function checkRegexp(o,regexp,n) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}
	
	$('#modifyCategorie')
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	).mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});
	
	$.ajax({
		type : 'Get',
		url : '/qRH-WS/services/categorie/getAll',
		async : false,
		dataType : 'json',
		success : function(result, textStatus, jqXHR) {
			jQuery.each(result, function() {
				aDataSet.push(eval([this.idCategorie, this.libelleCategorie, updateImg, deleteImg]));
			});
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(xhr.responseText);
		}
	});
	
	$('#tableContainer').html( '<table class="display" id="dataTableCategorie"></table>' );
	
	$('#dataTableCategorie').dataTable( {
		"aaData": aDataSet,
		"aoColumns": [
            { "sTitle": "Id" },
			{ "sTitle": "Categorie" },
			{ "sTitle": "Modifier" },
			{ "sTitle": "Supprimer" }
			]
	} );
	
	$('#dataTableCategorie tbody tr').each(
		function() {
			
	        var nTds = $('td', this);
	        var idCategorie = $(nTds[0]).text();
	        var libelleCategorie = $(nTds[1]).text();
	        
	        $('td', this).slice(-1).click(
        		function() {
		        	var answer = confirm("Voulez-vous vraiment supprimer cette categorie ?");
					if(answer) {
						$.ajax({
							type : 'Delete',
							url : '/qRH-WS/services/categorie/delete',
							async: false,
							data : idCategorie,
							dataType : 'json',
							success : location.reload()
						});
		        	};
		        }
	        );
	        
	        $('td', this).slice(-2,-1).click(
	        		function() {
	        			openDialog(idCategorie, libelleCategorie, "update");
			        }
	        );
	    }
	);
	
	
};

